//基础库
import request from '@/utils/request'
import {ElLoading} from "element-plus"
import {reactive,ref} from 'vue'
import {message} from '@/utils/message.js'
var loading = ref(loading)

export function Loading() {
    loading = ElLoading.service({
        fullscreen: true, text: '加载中...',
    })
}

export function MSuccess(res) {
    if (typeof res.data == "string") {
        message.success(res.data)
    } else {
        if ("msg" in res.data) {
            message.error(res.data.msg)
        }
    }
}


export class BaseApi {
    base: string = ""
    info: string = this.base + "/info"
    dialogVisible = ref(false)
    data = ref([])
    total = ref(0)
    pages = ref({"page":1,"size":15,"total":0,"query": {}})

    Rmes = async (res)=> {
        if (typeof res.data == "string") {
            message.success(res.data)
        } else {
            console.log(res)
            if ("msg" in res.data) {
                message.error(res.data.msg)
            }
        }}

    Create = async (data) => {
        var resp = reactive({})
        await request({
            url: this.base, method: 'post', data: data,
        }).then((response) => {
            MSuccess(response)
            resp = response.data
        })
        await this.List()
        return resp
    }

    Detail = async (name) => {
        var url = this.base + "/info/" + name
        var data = reactive({})
        Loading()
        await request({
            url: url, method: 'get', param: {id: data},
        }).then((response) => {
            MSuccess(response)
            data = response.data
        })
        loading.close()
        return data
    }

    List = async () => {
        var page = this.pages.value.page
        var size = this.pages.value.size
        var query = this.pages.value.query

        var url = this.base

        var param = ""

        if (page != undefined) {
            param = param + "&page" + "=" + page
        }

        if (size != undefined) {
            param = param + "&size" + "=" + size
        }

        if (query != undefined) {
            for (var k in query) {
                if (query[k] != undefined) {
                    param = param + "&" + k + "=" + query[k]
                }
            }
        }
        if (param.length > 0) {
            url = url + "?" + param
        }
        Loading()
        var data = reactive([])
        data = await request({
            url: url, method: 'get',
        }).then(async (response) => {
            loading.close()
            var data =  await response.data
            this.data.value = await data.data
            this.pages.value.total = await data.total
            return  response.data
        })
        loading.close()
        return data
    }

    Modify = async (data) => {
        await request({
            url: this.base, method: 'put', data: data
        }).then((response) => {
            MSuccess(response)
        })
        await this.List()
    }

    Delete = async (name) => {
        var url = this.base + "/" + name
        await request({
            url: url, method: 'delete'
        }).then((response) => {
            MSuccess(response)
        })
        await this.List()
    }

}

export class BaseOper extends BaseApi {
    Switch = async (data) => {
        var url = this.base + "/switch"
        return await request({
            url: url, method: 'put', data: data,
        })
    }

    Copy = async (data) => {
        var url = this.base + "/copy"
        return await request({
            url: url, method: 'post', data: data,
        }).then((response) => {
            MSuccess(response)
        })
    }

    Run = async (name) => {
        var url = this.base + "/run"
        return await request({
            url: url, method: 'put', data: {"name": name},
        }).then((response) => {
            MSuccess(response)
        })
    }

    Stop = async (name) => {
        var url = this.base + "/stop"
        return await request({
            url: url, method: 'put', data: {"name": name},
        }).then((response) => {
            MSuccess(response)
        })
    }
}

export const user = new class User extends BaseApi {
    base = '/api/v1/user'
    login = '/api/v1/login'
    logout = '/api/v1/logout'

    Login = async (data) => {
        return await request({
            url: this.login,
            method: 'post',
            data: data,
        })
    }

    Logout = async () => {
        return await request({
            url: this.logout,
            method: 'post',
            headers: {
                'Content-Type': 'application/json;charset=UTF-8'
            }
        })
    }
}